package me.work.pay.jsyl.mvp.ui.adapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;

import me.work.pay.jsyl.app.Constants;
import me.work.pay.jsyl.app.utils.Utils;
import me.work.pay.jsyl.mvp.ui.activity.GroupListActivity;
import me.work.pay.jsyl.mvp.ui.activity.WifiActivity;
import me.work.pay.jsyl.mvp.ui.activity.WifiLandActivity;

public class MyBroadcastReceiver extends BroadcastReceiver {
    private int true_index = 0;
    private boolean now_status = false;

    /**
     * 检测网络是否连接
     */
    private boolean NetworkAvailable(Context context) {
        try {
            Thread.sleep(600);
            //得到网络连接信息

            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            if (manager != null) {

                // 获取NetworkInfo对象

                NetworkInfo networkInfo = manager.getActiveNetworkInfo();

                //去进行判断网络是否连接

                if (networkInfo != null) {
                    return true;
                } else {
                    if (networkInfo != null) {
                        networkInfo.isAvailable();
                    }
                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return false;

    }

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent != null
                && WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Log.e("======", "我是结果：" + NetworkAvailable(context));
            Parcelable parcelable = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            NetworkInfo info = (NetworkInfo) parcelable;
            Log.e("======", "parcelable:" + new Gson().toJson(info));
            if (parcelable != null) {
                boolean result = Utils.isConnectIsNormal(context);
                if (result != now_status) {
                    now_status = result;
                    true_index = 0;
                } else {
                    true_index++;
                    if (true_index == 2 && now_status) {
                        String now_page = SPUtils.getInstance().getString(Constants.sp_page_value);
                        //配网的时候才播放声音
                        if (now_page.equals("5")) {
                            Log.e("======", "---------:" + now_status);
                            GroupListActivity.activity.soundMaker.makeSound("3", "24");//联网成功
                            if (WifiActivity.main != null) {
                                WifiActivity.main.killMyself();
                            }
                            if (WifiLandActivity.main != null) {
                                WifiLandActivity.main.killMyself();
                            }
//                            intent = new Intent(context, GroupListActivity.class);
//                            context.startActivity(intent);
                        }
                    } else if (!now_status && info.getExtraInfo().contains("ssid")) {
//                        new SoundMaker(context).makeSound("3","25");//联网失败
                    }
                }
                Log.e("======结果", true_index + "---------:" + now_status);

            }
        }
    }
}